﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ana_Form
{
    public partial class Form_ModelGirdisi : Form
    {
        public Form_ModelGirdisi(ref DepodataDataContext DB)
        {
            InitializeComponent();
            db = DB;
        }

        public Form_ModelGirdisi(ref DepodataDataContext DB, int mrkid)
        {
            InitializeComponent();
            /* TODO: Complete member initialization*/
            db = DB;
            marka = (from x in db.Markas
                     where x.id == mrkid
                     select x).FirstOrDefault();
            lblmarka.Text = marka.Adi;
        }
        DepodataDataContext db;
        Marka marka;
        Model model;

        public Model Model
        {
            get { return model; }
            set { model = value; }
        }
        private void btnmarka_Click(object sender, EventArgs e)
        {
            Form_Marka frm = new Form_Marka(ref db);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                marka = frm.Marka;
                lblmarka.Text = marka.Adi;
            }
        }

        private void lblmarka_TextChanged(object sender, EventArgs e)
        {
            if (lblmarka.Text == "")
            {
                txtmodeladi.Enabled = false;
                txtmodelno.Enabled = false;
            }
            else
            {
                txtmodeladi.Enabled = true;
                txtmodelno.Enabled = true;
            }
        }

        private void Form_modelGirdisi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Escape)
            {
                btniptal_Click(null, null);
            }
        }

        private void btniptal_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btntamam_Click(object sender, EventArgs e)
        {
            Model = new Model();
            Model.Adi = txtmodeladi.Text;
            Model.Modelno = txtmodelno.Text;
            Model.Marka = marka;
            db.Models.InsertOnSubmit(Model);
            db.SubmitChanges();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtmodel_TextChanged(object sender, EventArgs e)
        {
            if (txtmodeladi.Text=="")
            {
                btntamam.Enabled = false;
            }
            else
            {
                btntamam.Enabled = true;
            }
        }
    }
}
